package weaver.formmode.customjavacode.modeexpand.dfhj.util;

import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.TimeUtil;
import weaver.general.Util;

import java.util.Map;
import java.util.Set;
import java.util.UUID;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2021-11-02
 * @Description: ${description}
 * @ModifyLog:
 **/

public class DfhjUtil {
    //获取formmodeid
    public static int getFormmodeId(String tableName) {
        RecordSet rs = new RecordSet();
        rs.executeQuery( "select m.id from modeinfo m left join workflow_bill b on m.formid = b.id where b.tablename = ?", tableName );
        rs.next();
        int formmodeid = Util.getIntValue( rs.getString( "id" ) );
        return formmodeid;
    }
    //建模权限重构
    public static void getNewModeRight(int userid, int formmodeid, int id) {
        ModeRightInfo modeRightInfo = new ModeRightInfo();
        modeRightInfo.setNewRight( true );
        modeRightInfo.editModeDataShare( userid, formmodeid, id  );
    }
    //获取UUID转id
    public static String getUUUID2ID(String tableName,String uuid) {
        RecordSet rs = new RecordSet();
        rs.executeQuery( "select id from "+tableName+" where uuid = ?", uuid );
        rs.next();
        String id = Util.null2String( rs.getString( "id" ) );
        return id;
    }
    //获取UUID
    public static String getUUID(){
        UUID MR_UUID = UUID.randomUUID();
        return MR_UUID.toString();
    }
    //将数据插入表单建模
    public static int insertModelData(int modeid, int userid, Map map){
        int dataid=0;
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        //查询该模块表名
        String sql = "select b.tablename,a.formid from modeinfo a left join workflow_bill b on a.formid=b.id where a.id=?";
        rs.executeQuery(sql,modeid);
        rs.next();
        String tablename= Util.null2String(rs.getString("tablename"));
        String uuid = UUID.randomUUID().toString();
        String insSql = "insert into "+tablename
                +"(uuid,modedatacreater,modedatacreatedate,modedatacreatetime,formmodeid)values(?,?,?,?,?)";
        Object[] params = {uuid, userid, TimeUtil.getCurrentDateString(), TimeUtil.getOnlyCurrentTimeString(), modeid};
        if(rs.executeUpdate(insSql,params)){
            rs1.executeQuery("select id from "+tablename+" where uuid=?",uuid);
            rs1.next();
            dataid= Util.getIntValue(rs1.getString("id"));
            if(dataid>0){
                //遍历数据 进行update
                String updatesql="update "+tablename+" set ";
                Set<String> keySet = map.keySet();
                for(String key : keySet){
                    updatesql+=key+"='"+map.get(key).toString()+"',";
                }
                if(updatesql.endsWith(",")){
                    updatesql=updatesql.substring(0, updatesql.length()-1);
                    updatesql+=" where id="+dataid;
                    rs.executeUpdate(updatesql);
                }
                //进行权限重构
                getNewModeRight(userid, modeid, dataid);
            }
        }
        return dataid;
    }

    public static void main(String[] args) {
//        int docid =-1;
//        int secid =-1;
//        DocManager dm = new DocManager();
//        dm.setId(docid);
//        dm.setSeccategory(secid);
//        dm.setUserid(user.getUID());
//        dm.setDocCreaterType(user.getLogintype());
//        dm.setDocdepartmentid(docdepartmentid);
//        dm.AddShareInfo();
//        DocViewer DocViewer = new DocViewer();
//        DocViewer.setDocShareByDoc(docid + "");
    }
}
